﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iChip.Hotspots.Models;
using iChip.Hostpots.Models;
using Newtonsoft.Json;
using iChip.Hostpots.Commons;

namespace iChip.Hotspots.Controllers
{
    public class FrontEndController : Controller
    {
        private BO.BOs _bo = new BO.BOs();
        // GET: FrontEnd
        public ActionResult Index(string called, string mac, int? user_type)
        {
            //var browser = Request.Browser;
            var objB = UserAgentParser.GetBrowser(Request.UserAgent);
            var objOS = UserAgentParser.GetOperatingSystem(Request.UserAgent);

            var model = new IndexObject();
            model = _bo.Select_Splash(called, mac);
            ViewBag.Hotspot_Mac = called;
            ViewBag.Mac = mac;
            ViewBag.UrlRedirect = model.Splash==null ? "" : model.Splash.Splash_UrlRedirect;
            ViewBag.User_Type = model.User_Type;
            ViewBag.Method_Type = string.IsNullOrEmpty(model.Method_Type) ? "No" : model.Method_Type ;
            var logs = new Logs
            {
                Hotspot_Mac = called,
                User_Mac = mac,
                Method_Type = "No",
                Log_OS = objOS.Name,
                Log_Brower = objB.Name,
                Log_Device = objOS.Name
            };

            _bo.Logs(logs);

            return View(model);
        }


        [HttpPost]
        public ActionResult PostInfo(List<RequestPostField> objRequest, string method_type, string hotspot_mac, string user_type, string mac, string urlRedirect)
        {
            int status = 0;
            string mess = string.Empty;
            try
            {
                
                _bo.Insert_Values(mac, objRequest);
                status = 1;

                var browser = Request.Browser;
                var logs = new Logs
                {
                    Hotspot_Mac = hotspot_mac,
                    User_Mac = mac,
                    Method_Type = method_type,
                    Log_OS = browser.Platform,
                    Log_Brower = browser.Type, //browser.Browser,
                    Log_Device = browser.IsMobileDevice ? browser.MobileDeviceModel : browser.Platform
                };

                //Update lai log
                _bo.Logs(logs);
                //update thong tin user
                _bo.UpdateUser(new User
                {
                    User_Mac = mac,
                    User_Type = Convert.ToInt32(user_type),
                    User_Status = true,
                    User_Birthday = DateTime.Now
                });


                return Json(new { Status = status, Message = mess, urlRedirect = urlRedirect });
            }
            catch(Exception ex)
            {
                mess = ex.Message;
                status = 0;
                return Json(new { Status = status, Message = mess, urlRedirect = urlRedirect });
            }
            
        }
    }
}